#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e4+100;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],p[maxn];
map<LL,LL>map1[maxn];
LL num[maxn],tot=0;
LL ksm(LL a,LL k){
    LL res=1;
    while(k>0){
        if(k&1) res=res*a%mod;
        k>>=1;
        a=a*a%mod;
    }
    return res%mod;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++){
    cin>>a[i];
  }
  for(LL i=1;i<=n;i++){
    cin>>p[i];
  }
  LL gcd=0;
  for(LL i=1;i<=n;i++){
    gcd=__gcd(gcd,a[i]);
  }
  if(gcd==1){
    cout<<gcd<<"\n";
  }
  else{
     tot=0;
    /// cout<<ksm(3,2)<<"\n";
     for(LL i=1;i<=n;i++){
        LL temp=a[i];
        for(LL j=2;j<=sqrt(temp);j++){
            if(temp%j==0){
                while(temp%j==0){
                    map1[i][j]++;
                    temp/=j;
                }
            }
        }
        if(temp>1) map1[i][temp]++;
     }
     for(LL i=2;i<=sqrt(gcd);i++){
        if(gcd%i==0){
            num[++tot]=i;
            while(gcd%i==0){
                gcd/=i;
            }
        }
     }
     if(gcd>1){
        num[++tot]=gcd;
     }
     LL ans=1;
     for(LL i=1;i<=tot;i++){
        LL sum=1e18;
        for(LL j=1;j<=n;j++){
            if(map1[j].count(num[i])){
                sum=min(sum,map1[j][num[i]]*p[j]);
            }
        }
    ///    debug(sum);
        if(sum!=0x3f3f3f3f){
            ans=(ans%mod*ksm(num[i],sum)%mod)%mod;
        }
     }
     cout<<ans<<"\n";
  }
return 0;
}
